package entry;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Properties;

import aima.core.search.framework.Problem;
import aima.core.search.framework.Search;
import domain.PacmanActionsFunction;
import domain.PacmanBoard;
import domain.PacmanGoalTest;
import domain.PacmanResultFunction;
import domain.SearchType;
import domain.randomRoosterGeneratie;


public class runRandomized 
{

	public void runTests()
	{
	
	
		int aantalExperimenten = 10000; //aantal keer dat we elke search uitvoeren
		int maxNGrootte = 6;
		
		String[][] outputTable = new String[maxNGrootte + 1][16]; 
		
		for(int n = 3; n <= maxNGrootte; ++n) //overloop bordgroottes
		{
			System.out.println("------------------------------N = " + n + "------------------------------");
			//zet n waarde in output table
			outputTable[n][0] = String.valueOf(n);
			


				
				
				
				

				
				
				//------------------------------------------------------------------------------
				
			for(int huidigZoekAlgoritme = 0; huidigZoekAlgoritme < 5; ++huidigZoekAlgoritme)
			{
			
				double    padKost		= 0.0;
				long   nodesExpanded  = 0;
				long   maxQueueSize   = 0;
				
			
				
				
				
				for(int i = 0; i < aantalExperimenten; ++i) //overloop aantalExperimenten
				{
					//genereer rooster
					/* DIT IS DUPLICATE CODE EN MOET VERSMELT WORDEN MET DE REST VAN DE APPLICATIE*/
					randomRoosterGeneratie rooster = new randomRoosterGeneratie(n);
					rooster.generateRooster();
					char[][] map= rooster.getRooster();
					
					PacmanBoard board = new PacmanBoard(map); // = state
					System.out.println("Before search:" );
					board.printBoard();
					
					//voer zoekalgoritmes uit
					//tel resultaten bij padkost, nodesExpanded & maxQ op 
					Properties instrumentation = null;
					
					
					Problem problem = new Problem(board, new PacmanActionsFunction(), new PacmanResultFunction(), new PacmanGoalTest());		
					Search search = null; 
					
					
					switch(huidigZoekAlgoritme)
					{
					case 0: //BFS tree
						System.out.println("[BFST]");
						outputTable[0][1] = "BFST";
						outputTable[0][6] = "BFST";
						outputTable[0][11] = "BFST";
						search = Application.createSearch(SearchType.BFSTree);
						break;
					case 1: //BFS graph
						System.out.println("[BFSG]");
						outputTable[0][2] = "BFSG";
						outputTable[0][7] = "BFSG";
						outputTable[0][12] = "BFSG";
						search = Application.createSearch(SearchType.BFSGraph);
						break;
					case 2: //DFS
						System.out.println("[DFS]");
						outputTable[0][3] = "DFS";
						outputTable[0][8] = "DFS";
						outputTable[0][13] = "DFS";
						search = Application.createSearch(SearchType.DFS);
						break;
					case 3: //ID
						System.out.println("[ID]");
						outputTable[0][4] = "ID";
						outputTable[0][9] = "ID";
						outputTable[0][14] = "ID";
						search = Application.createSearch(SearchType.ID);
						break;
					case 4: //A*
						System.out.println("[A*]");
						outputTable[0][5] = "A*";
						outputTable[0][10] = "A*";
						outputTable[0][15] = "A*";
						search = Application.createSearch(SearchType.AStar);
						break;
					}
				
					instrumentation = Application.performSearch(problem, search);
					
				
					
					String strPathCost = (String)instrumentation.get("pathCost");
					String strNodesExpanded = (String)instrumentation.get("nodesExpanded"); 
					String strMaxQueueSize = (String)instrumentation.get("maxQueueSize"); 
					
					
				
					if(strPathCost != null)
					{
						
						padKost += Float.valueOf((String)instrumentation.get("pathCost"));
					}
					
					if(strNodesExpanded != null)
					{
						nodesExpanded += Integer.valueOf((String)instrumentation.get("nodesExpanded"));
					}
					
					if(strMaxQueueSize != null)
					{
						maxQueueSize += Integer.valueOf((String)instrumentation.get("maxQueueSize"));
					}
						
						
		
					
					
					
					

				
					System.out.println("-> END OF EXPERIMENT " + i + "----------");
				}
				
				//maak van totalen gemiddeldes en druk informatie af
				padKost /= aantalExperimenten;
				nodesExpanded /= aantalExperimenten;
				maxQueueSize /= aantalExperimenten;
				
				
				switch(huidigZoekAlgoritme)
				{
				case 0: //BFS tree
					outputTable[n][1] = String.valueOf(maxQueueSize);
					outputTable[n][6] = String.valueOf(nodesExpanded);
					outputTable[n][11] = String.valueOf(padKost); 
					
					break;
				case 1: //BFS graph
					outputTable[n][2] = String.valueOf(maxQueueSize);
					outputTable[n][7] = String.valueOf(nodesExpanded);
					outputTable[n][12] = String.valueOf(padKost);
					break;
					
				case 2: //DFS
					outputTable[n][3] = String.valueOf(maxQueueSize);
					outputTable[n][8] = String.valueOf(nodesExpanded);
					outputTable[n][13] = String.valueOf(padKost); 
					break;
				case 3: //ID
					outputTable[n][4] = String.valueOf(maxQueueSize);
					outputTable[n][9] = String.valueOf(nodesExpanded);
					outputTable[n][14] = String.valueOf(padKost);
					break;
					
				case 4: //A*
					outputTable[n][5] = String.valueOf(maxQueueSize);
					outputTable[n][10] = String.valueOf(nodesExpanded);
					outputTable[n][15] = String.valueOf(padKost); 
					break;
				}
				
				
				
				System.out.println("gem. padkost = " + padKost);
				System.out.println("gem. nodes expandes = " + nodesExpanded);
				System.out.println("gem. queue size = " + maxQueueSize);
				
			
				
			}
			

		}
	
		
		
		
		
		
		//print output naar file
		Writer writer = null;

		try 
		{
		    writer = new BufferedWriter(new OutputStreamWriter(
		          new FileOutputStream("OUTPUT.txt"), "utf-8"));
		    
		    for(int i = 0;  i < outputTable.length; ++i)
		    {
		    	
		    	for(int j = 0; j < outputTable[0].length; ++j)
		    	{
		    		writer.write("\t\t" + outputTable[i][j]);
		    	}
		    	
		    	writer.write("\r\n");
		    	
		    }
		    
		} catch (IOException ex) {
		  // report
		} finally {
		   try {writer.close();} catch (Exception ex) {}
		}
		
	}
	

	
	
}
	

